<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2021-03-31 15:08:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-edit">
        <el-dialog
        :title="$t('devUpgrade.VersionToUp')"
        :visible="visibleUpgrade"
        width="700px"
        @close="()=>{$store.commit('setVisibleUpgrade',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="20">
                    <el-form ref="form" :model="form" label-width="240px" >
                        <!-- 版本号 -->
                        <el-form-item :label="$t('version.versionAdd.versionNum')" prop="wifiSns" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.wifiSns" style="width:100%" filterable :placeholder="$t('pleaseSelect')">
                                <el-option
                                v-for="item in valueList"
                                :key="item.taskId"
                                :label="item.longVersion"
                                :value="item.taskId">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item :label="$t('remark')" prop="remark" :rules="[{ required: false, message: $t('index_js_notNull')}]">
                            <el-input v-model="form.remark" type='textarea' disabled="disabled" rows="5"></el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleUpgrade',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {
    },
    props: {
        rowEditData:{
            type:Object,
            default:()=>{return {}}
        },
        editWifiSn:{
            type:String,
            default:''
        },
        deviceType:{
            type:String,
            default:''
        },
        deviceClassify:{
            type:Array,
            default:()=>{return []}
        },
    },
    data() {
        return {
            form:{
                version:"",
                remark:'',
            },
            valueList: []
        };
    },
    computed: {
        visibleUpgrade(){
            return this.$store.state.sysStore.visibleUpgrade
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {
        console.log(this.deviceClassify,'----deviceClassify')
        this.updateGetUpVersion()
    },
    methods: {
        //点击升级
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.updateUpdateVersion()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
        //获取版本列表
        async updateGetUpVersion(){
            let res = await this.API.updateGetUpVersion({
                wifiSN: this.editWifiSn,
                type: this.deviceClassify[0].value,
            })
            this.valueList = res
            if(!res.length){
                this.comm.showMsg(false,this.$t('devUpgrade.noVersionToChoose'))
            }
        },
        //升级到指定版本
        async updateUpdateVersion(){
            let res = await this.API.updateUpdateVersion({
                wifiSns: this.form.wifiSns + "#" + this.editWifiSn //版本号 # wifiSN
            })
            if(res.success){
                this.comm.showMsg(res.success,this.$t('devUpgrade.waitForUpgrade'))
                this.$store.commit('setVisibleUpgrade',false)
                this.$emit('uploadData')
                this.form.wifiSns = ''
            }else{
                this.comm.showMsg(res.success)
            }
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-edit{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
    .el-textarea.is-disabled .el-textarea__inner {
        background-color: #f5f5f5;
        color: #fea429;
    }
}
</style>
